<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>颜色分类, 按照红色(0)、白色(1)、蓝色(2)顺序排列</title>
</head>
<body>
  <script>
    function fn(nums) {
      let i = 0, num = nums.length;
      // 循环执行 nums 的长度 - 2 的数量
      while (i < num) {
        if (nums[i] == 0) {
          nums.splice(i++, 1);
          nums.unshift(0);
        } else if (nums[i] == 2) {
          nums.splice(i, 1);
          nums.push(2)
          num--;
        } else i++
      }
      return nums
    }
    console.log(fn([2, 0, 2, 1, 1, 0]))
  </script>
</body>
</html>